﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using ZCrawler;

namespace ZCrawlerManager
{
    public partial class FrmMapper : DevExpress.XtraEditors.XtraForm
    {
        private Point p;
        public ZPublisherMapper mapper;
        public DialogResult result;
        public FrmMapper()
        {
            InitializeComponent();
            mapper = new ZPublisherMapper()
            {
                OriginalKeys = new List<string>() { "test1","test2","test3"},
                MappedKeys = new List<string>() { "","",""}
            };
            DataTable dt = new DataTable("data");
            dt.Columns.Add("OriginalKeys");
            dt.Columns.Add("MappedKeys");
            mapper.OriginalKeys.ForEach(k =>
                {
                    DataRow row = dt.NewRow();
                    row["OriginalKeys"] = k;
                    dt.Rows.Add(row);
                    treeList1.Nodes.Add(row);
                });
        }
        public FrmMapper(string[] ori_keys,string[] map_keys)
        {
            InitializeComponent();
            mapper = new ZPublisherMapper();
            DataTable dt = new DataTable("data");
            dt.Columns.Add("OriginalKeys");
            dt.Columns.Add("MappedKeys");
            ori_keys.ToList().ForEach(k =>
            {
                DataRow row = dt.NewRow();
                row["OriginalKeys"] = k;
                dt.Rows.Add(row);
                treeList1.Nodes.Add(row);
            });
            listBoxControl1.Items.AddRange(map_keys);
        }


        private void listBoxControl1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void listBoxControl1_MouseDown(object sender, MouseEventArgs e)
        {
            ListBoxControl c = sender as ListBoxControl;
            p = new Point(e.X, e.Y);
            int selectedIndex = c.IndexFromPoint(p);
            if (selectedIndex == -1)
                p = Point.Empty;
        }

        private void listBoxControl1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                if ((p != Point.Empty) && ((Math.Abs(e.X - p.X) > 5) || (Math.Abs(e.Y - p.Y) > 5)))
                    listBoxControl1.DoDragDrop(listBoxControl1.SelectedItem, DragDropEffects.Move);
        }

        private void treeList1_DragDrop(object sender, DragEventArgs e)
        {
            TreeListNode node = treeList1.CalcHitInfo(treeList1.PointToClient(new Point(e.X, e.Y))).Node;
            if (node == null) return;
            var data = e.Data.GetData(typeof(String));
            //Find
            int idx = treeList1.Nodes.OfType<TreeListNode>().ToList().FindIndex(n =>
                {
                    return node.GetValue(col_map_node).ToString().Equals(data);
                });
            if(idx != -1)
            {
                treeList1.Nodes[idx].SetValue(col_map_node, "");
            }
            node.SetValue(col_map_node, data.ToString());
        }

        private void treeList1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //Construct mapper
            foreach(TreeListNode node in treeList1.Nodes)
            {
                if(string.IsNullOrEmpty(node.GetValue(col_map_node).ToString().Trim()))
                {
                    XtraMessageBox.Show("Not every key is got mapped");
                    return;
                }
                mapper.Add(node.GetValue(col_ori_node).ToString(), node.GetValue(col_map_node).ToString());
            }
            result = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            result = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}